import mock from 'mockjs'
// import mock from 'better-mock'
// import mock from 'better-mock/dist/mock.mp'
// console.log(111,mock.Random.name())
let baseUrl = 'http://localhost:8080'
const random = mock.Random
mock.mock(`${baseUrl}/mock/test`, {
	"code": 1,
	"msg": "succsee",
	'data|1-10': [{
		'id|+1': 1,
		'name': '@name',
	}]
})

const usersInfo = [{
	userId: '1',
	username: 'liming',
	password: '123',
	email: '11111@163.com',
	userPhone: '18888888888',
	avatarImgSrc: 'https://img0.baidu.com/it/u=53079408,1616173051&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1682182800&t=b869c381403c470557d3a09a9aeb266b'
}]
// 登录信息
mock.mock(`${baseUrl}/mock/login`, 'post', (data) => {
	data.body = JSON.parse(data.body)
	let obj = {
		code: 200,
		userInfo: {
			username: data.body.username,
		},
		'token': mock.Random.word(6, 10)
	}
	const isExistAccount = usersInfo.some(item => {
		if (data.body.username == item.username && item.password == data.body.password) {
			obj.userInfo.userId = item.userId
			obj.userInfo.email = item.email
			obj.userInfo.avatarImgSrc = item.avatarImgSrc
			obj.userInfo.userPhone = item.userPhone
			return true
		}
	})
	if (isExistAccount) {
		return obj
	} else {
		const err = {
			errMsg: '用户名或密码输入错误'
		}
		return err
	}
})
// 修改用户信息
mock.mock(`${baseUrl}/mock/updateUserInfo`, 'post', (data) => {
	data.body = JSON.parse(data.body)
	let obj = {}
	usersInfo.some(item => {
		if (item.userId == data.body.userId) {
			item = {
				...item,
				...data.body
			}
			const {
				password,
				...rest
			} = item
			console.log(222, data.body)
			obj = rest
			return true
		}
	})
	return {
		code: 200,
		info: obj
	}
})
const updateUserInfo = (userId, updateKey, updateValue) => {
	let obj = {}
	usersInfo.some(item => {
		if (item.userId == userId) {
			item[updateKey] = updateValue
			console.log(111, item)
			const {
				password,
				...rest
			} = item
			obj = rest
			return true
		}
	})
	return obj
}
// 修改用户姓名
mock.mock(`${baseUrl}/mock/updateUserName`, 'post', (data) => {
	data.body = JSON.parse(data.body)
	let obj = updateUserInfo(data.body.userId, 'username', data.body.username)
	return {
		code: 200,
		info: obj
	}
})
// 更换手机号
mock.mock(`${baseUrl}/mock/updateUserPhone`, 'post', (data) => {
	data.body = JSON.parse(data.body)
	let obj = updateUserInfo(data.body.userId, 'userPhone', data.body.userPhone)
	return {
		code: 200,
		info: obj
	}
})
// 更换邮箱
mock.mock(`${baseUrl}/mock/updateUserEmail`, 'post', (data) => {
	data.body = JSON.parse(data.body)
	let obj = updateUserInfo(data.body.userId, 'email', data.body.email)
	return {
		code: 200,
		info: obj
	}
})
// 修改密码
mock.mock(`${baseUrl}/mock/updatePassWord`, 'post', (data) => {
	data.body = JSON.parse(data.body)
	let obj = updateUserInfo(data.body.userId, 'password', data.body.password)
	return {
		code: 200,
		info: obj
	}
})
// 注册用户
mock.mock(`${baseUrl}/mock/register`, 'post', (data) => {
	data.body = JSON.parse(data.body)
	usersInfo.push({
		...data.body.usersInfo,
		id: random.id()
	})
	return {
		code: 200,
		msg: '注册成功'
	}
})
// 轮播图
mock.mock(`${baseUrl}/mock/swiperImages`, 'post', {
	code: 200,
	list: [{
			id: random.id(),
			imageSrc: 'https://img1.baidu.com/it/u=2356609896,878501075&fm=253&app=138&size=w931&n=0&f=GIF&fmt=auto?sec=1681578000&t=4e55da2b2e641ea489f0c94732db7b0e'
		},
		{
			id: random.id(),
			imageSrc: 'https://img0.baidu.com/it/u=3845949282,809789711&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=213'
		},
		{
			id: random.id(),
			imageSrc: 'https://img1.baidu.com/it/u=2888917256,3027125083&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500'
		}
	]
})
// 商品分类
mock.mock(`${baseUrl}/mock/goodsClassification`, 'post', {
	code: 200,
	"list|8": [{
		"id|+1": 1,
		"name|+1": ['水果蔬菜', '肉食蛋品', '海鲜水产', '酒水饮料', '休闲零食', '烘焙面点', '粮油调味', '鲜活水产'],
		"code|+1": [1, 2, 3, 4, 5, 6, 7, 8],
		"imageSrc|+1": [
			'https://img2.baidu.com/it/u=2076448801,2145932011&fm=253&fmt=auto&app=138&f=JPEG?w=596&h=500',
			'https://img2.baidu.com/it/u=2951932486,4225238111&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
			'https://img1.baidu.com/it/u=678831635,885834662&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
			'https://img1.baidu.com/it/u=1857738501,391220293&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=682',
			'https://img0.baidu.com/it/u=1751957613,2547887452&fm=253&fmt=auto&app=138&f=JPEG?w=480&h=480',
			'https://img0.baidu.com/it/u=1789703436,516967409&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
			'https://img0.baidu.com/it/u=3693782802,1077455002&fm=253&fmt=auto&app=138&f=JPEG?w=490&h=347',
			'https://img1.baidu.com/it/u=1857303411,2779314404&fm=253&fmt=auto&app=138&f=JPEG?w=350&h=350'
		]
	}, ]
})
// 商品库
const goodsList = mock.mock({
	"list|8": [{
		"id|+1": 1,
		"name|+1": ['苹果', '鸡蛋', '龙虾', '水饺', '麻辣王子', '面包', '金龙鱼食用油', '罗非鱼'],
		"classification|+1": [1, 1, 2, 3, 5, 6, 7, 8],
		"details|+1": ['苹果详情', '鸡蛋详情', '水饺详情', '辣条详情', '现烤面包', '植物油', '现捞活鱼'],
		"recommend|+1": [1, 1, 1, 1, 1, 1, 1, 1],
		"price|+1": [2.88, 3.88, 4.88, 5.88, 6.88, 7.88, 8.88, 12.8],
		"sales|+1": [20, 21, 22, 23, 24, 35, 36, 446],
		"inventory|+1": [10, 100, 100, 100, 100, 100, 100, 100],
		"addToCartNum|+1": [1, 0, 1, 1, 0, 0, 1, 1],
		"cartChecked|+1": [true, false, true, false, true, false, true, false],
		"cartValue|+1": ["pingguo", "jidan", "longxia", "shuijiao", "malawangzi", "mianbao",
			"shiyonyou", "luofeiyu"
		],
		"isCollection|+1": [0, 1, 1, 1, 1, 1, 1, 1],
		"specification|+1": ["500g/份", "500g/份", "500g/份", "500g/份", "500g/份", "500g/份", "500g/份",
			"500g/份"
		],
		"brand|+1": ["富士山", "一品鲜", "一品鲜", "一品鲜", "一品鲜", "一品鲜", "一品鲜", "一品鲜"],
		"service|+1": ["品质保证，不满意包退", "品质保证，不满意包退", "品质保证，不满意包退", "品质保证，不满意包退", "品质保证，不满意包退",
			"品质保证，不满意包退", "品质保证，不满意包退", "品质保证，不满意包退"
		],
		"imageSrc|+1": [
			'https://img0.baidu.com/it/u=2931543060,247992618&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1681578000&t=68f22c47b082fc01238f034058664831',
			'https://img0.baidu.com/it/u=1385469241,1455748898&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1681578000&t=8000f6fbdcd32cc1b36661ab4455045d',
			'https://img0.baidu.com/it/u=700935504,460040333&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1681578000&t=5a496b5aa1cd98d054b54ece459df5f0',
			'https://img0.baidu.com/it/u=1265934425,942013040&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1681578000&t=899bc211e55bf6f3ff5914132b71f31e',
			'https://img2.baidu.com/it/u=3499691880,4154189660&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1681578000&t=52d535cc551215eca52b0ffbd608c9de',
			'https://img1.baidu.com/it/u=3633017278,1481167167&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1681578000&t=7d3b21b31e1e927acc7f7c1b340e283a',
			'https://img0.baidu.com/it/u=3693782802,1077455002&fm=253&fmt=auto&app=138&f=JPEG?w=490&h=347',
			'https://img1.baidu.com/it/u=256582457,4155512630&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1681578000&t=19a4b1b46663e8533bc11d7035c02af4'
		]
	}, ]
})
// 推荐商品
mock.mock(`${baseUrl}/mock/goodsRecommend`, 'post', {
	code: 200,
	list: goodsList.list.filter(item => item.recommend)
})

// 根据类型查商品
mock.mock(`${baseUrl}/mock/goodsByClassification`, 'post', (data) => {
	data.body = JSON.parse(data.body)
	return {
		code: 200,
		list: goodsList.list.filter(item => item.classification == data.body.classification)
	}
})
// 查询全部商品
mock.mock(`${baseUrl}/mock/goodsAll`, 'post', {
	code: 200,
	list: goodsList.list
})
// 查询购物车商品
mock.mock(`${baseUrl}/mock/goodsInCart`, 'post', (data) => {
	return {
		code: 200,
		list: goodsList.list.filter(item => item.addToCartNum)
	}
})
// 查询收藏商品
mock.mock(`${baseUrl}/mock/goodsCollect`, 'post', (data) => {
	return {
		code: 200,
		list: goodsList.list.filter(item => item.isCollection)
	}
})

// 修改购物车勾选状态
mock.mock(`${baseUrl}/mock/updataCartChecked`, 'post', (data) => {
	data.body = JSON.parse(data.body)
	goodsList.list.some(item => {
		if (item.id == data.body.id) {
			item.cartChecked = !item.cartChecked
			return true
		}
	})
	return {
		code: 200,
		msg: '修改成功'
	}
})
// 修改加入购物车数量
mock.mock(`${baseUrl}/mock/updataAddToCartNum`, 'post', (data) => {
	data.body = JSON.parse(data.body)
	goodsList.list.some(item => {
		if (item.id == data.body.id) {
			if (data.body.hasOwnProperty('cartNum')) {
				item.addToCartNum = data.body.cartNum
				if (data.body.cartNum == 0) {
					item.cartChecked = true
				}
			} else {
				item.addToCartNum = item.addToCartNum + 1
			}
			return true
		}
	})
	return {
		code: 200,
		msg: '修改成功'
	}
})
// 商品详情
mock.mock(`${baseUrl}/mock/goodsDetail`, 'post', (data) => {
	data.body = JSON.parse(data.body)
	let list = goodsList.list.filter(item => item.id == data.body.id)
	return {
		code: 200,
		detailInfo: list[0],
		cartInfo: goodsList.list.filter(item => item.addToCartNum),
		collectionInfo: goodsList.list.filter(item => item.isCollection),
		detailRichText: [{
			name: 'div',
			attrs: {
				class: 'div-class',
				style: 'line-height: 60px; color: red; text-align:center;'
			},
			children: [{
					type: 'text',
					text: '图文信息'
				},
				{
					type: 'node',
					name: 'img',
					attrs: {
						src: list[0].imageSrc,
						style: 'width:100%;height:300px'
					}
				}
			]
		}]
	}
})

// 收获收货地址列表
const addressList = mock.mock({
	"list|3": [{
		"addressId|+1": [1, 2, 3],
		"address|+1": ["湖南省长沙市岳麓区aaa", "湖南省长沙市岳麓区bbb", "湖北省武汉市江岸区ccc"],
		"detailAddress|+1": ["aaa", "bbb", "ccc"],
		"city|+1": ["长沙市", "长沙市", "武汉市"],
		"province|+1": ["湖南省", "湖南省", "湖北省"],
		"county|+1": ["岳麓区", "岳麓区", "江岸区"],
		"addressName|+1": ["小明", "小张", "小张"],
		"addressPhone|+1": ["18888888888", "18999999999", "18999999999"],
		"isDefault|+1": [false, true, false],

	}],
})

// 查询收货地址列表
mock.mock(`${baseUrl}/mock/getAddressList`, 'post', (data) => {
	return {
		code: 200,
		list: addressList.list
	}
})
// 查询收货地址详情
mock.mock(`${baseUrl}/mock/getAddressDetail`, 'post', (data) => {
	data.body = JSON.parse(data.body)
	let obj = {}
	addressList.list.some(item => {
		if (item.addressId == data.body.addressId) {
			obj = item
			return true
		}
	})
	return {
		code: 200,
		data: obj
	}
})
// 添加收货地址信息
mock.mock(`${baseUrl}/mock/addAddress`, 'post', (data) => {
	data.body = JSON.parse(data.body)
	let maxId = addressList.list[0].addressId
	addressList.list.map(item => {
		if (maxId < item.addressId) {
			let id = item.addressId;
			[maxId, id] = [id, maxId]
		}
	})
	const addressId = maxId + 1
	const obj = {
		addressId,
		...data.body,
		address: data.body.province + data.body.city + data.body.county + data.body.detailAddress
	}
	console.log(111, obj)
	addressList.list.some((item, index, arr) => {
		Object.keys(data.body).map(ele => {
			if (ele == 'isDefault' && data.body[ele]) {
				arr.some(item2 => {
					if (item2.isDefault) {
						item2.isDefault = false
						return true
					}
				})
			}
		})
	})
	addressList.list.push(obj)
	return {
		code: 200,
		msg: '添加成功'
	}
})
// 修改收货地址信息
mock.mock(`${baseUrl}/mock/updateAddress`, 'post', (data) => {
	data.body = JSON.parse(data.body)
	addressList.list.some((item, index, arr) => {
		if (item.addressId == data.body.addressId) {
			Object.keys(data.body).map(ele => {
				if (ele == 'isDefault' && data.body[ele]) {
					arr.some(item2 => {
						if (item2.isDefault) {
							item2.isDefault = false
							return true
						}
					})
				}
				item[ele] = data.body[ele]
			})
			return true
		}
	})
	return {
		code: 200,
		msg: '修改成功'
	}
})
// 移除收货地址
mock.mock(`${baseUrl}/mock/delAddress`, 'post', (data) => {
	data.body = JSON.parse(data.body)
	addressList.list = addressList.list.filter(item => {
		return item.addressId != data.body.addressId
	});
	console.log(111, addressList.list, data.body)
	return {
		code: 200,
		msg: '删除成功'
	}
})
// 省数据
const province = mock.mock({
	"list|2": [{
		"id|+1": 100,
		"name|+1": ['湖南省', '湖北省'],
	}],
})
// 市数据
const city = mock.mock({
	"list|5": [{
		"pid|+1": [100, 100, 100, 101, 101],
		"id|+1": 100,
		"name|+1": ['岳阳市', '株洲市', '长沙市', '武汉市', '孝感市'],
	}],
})
// 区县数据
const county = mock.mock({
	"list|8": [{
		"pid|+1": [100, 101, 101, 102, 102, 102, 103, 104],
		"id|+1": 100,
		"name|+1": ['岳阳楼区', '天元区', '荷塘区', '岳麓区', '天心区', '雨花区', '江岸区', '孝南区'],
	}],
})
// 查询省数据
mock.mock(`${baseUrl}/mock/findProvince`, 'post', (data) => {
	return {
		code: 200,
		list: province.list
	}
})
// 查询市数据
mock.mock(`${baseUrl}/mock/findCity`, 'post', (data) => {
	return {
		code: 200,
		list: city.list
	}
})
// 查询区县数据
mock.mock(`${baseUrl}/mock/findCounty`, 'post', (data) => {
	return {
		code: 200,
		list: county.list
	}
})
// 修改收藏状态
mock.mock(`${baseUrl}/mock/updateCollectionStatus`, 'post', (data) => {
	data.body = JSON.parse(data.body)
	goodsList.list.some(item => {
		if (item.id == data.body.id) {
			item.isCollection = item.isCollection ? 0 : 1
			return true
		}
	})
	return {
		code: 200,
		msg: '修改收藏状态成功'
	}
})